var armorMessage = new i18nMessage('../../servlet/I18nMessageServlet?keyPrefix=ui.armor');
var message = new i18nMessage('../../servlet/I18nMessageServlet?keyPrefix=message');
var customer = new i18nMessage('../../servlet/BiFranceCustomerServlet?action=5');
var ctArmorTypeXmlLoader = new i18nMessage('../../servlet/CodeTableServlet?name=CtArmorType');
var ctArmorSetXmlLoader = new i18nMessage('../../servlet/CodeTableServlet?name=CtArmorSet');
var ctBusinessTypeXmlLoader = new i18nMessage('../../servlet/CodeTableServlet?name=CtBusinessType');

var armorStore;
var armorGrid;
var armorWindow;

var selectArmor = function(selectCallback){
    if (!armorWindow) {
        armorWindow = new Ext.Window({
            title: message.get('message.combo.pleaseselect'),
            layout: 'fit',
            width: 900,
            height: 605,
            closeAction: 'hide',
            plain: true,
            modal: true,
            items: [{
                id: 'buttonPanel',
                region: 'center',
                bodyStyle: {
                    background: '#ffffff',
                    padding: '7px'
                },
                html: '<div id="armorDiv"></div>'
            }]
        });
    }
    armorWindow.show();
    
    if (armorGrid) {
		armorStore.reload();
		return;
	}
    armorStore = new Ext.data.Store({
        url: '../../servlet/BiArmorServlet?action=4',
        
        reader: new Ext.data.XmlReader({
            record: 'item',
            id: 'id',
            totalRecords: 'itemTotal'
        }, [ 
         'id', 'index', 'reference', 'ctArmorType','ctArmor1Type','armorTypeE', 'ctArmorSet', 'ctJauge', 'armor',
         'photo1Name', 'photo2Name', 'photo3Name', 'photo4Name', 'comment']),
        
        remoteSort: true
    });
    
    armorStore.setDefaultSort('id', 'desc');
    
    var armorFilters =  new Ext.grid.GridFilters({
		 filters:[
					    {type: 'string',  dataIndex: 'reference'},
					    {type: 'string', dataIndex: 'armor'},
						{type: 'list',  dataIndex: 'ctArmorType', options: ctArmorTypeXmlLoader.getCtArray() },
						{type: 'list',  dataIndex: 'ctArmorSet', options: ctArmorSetXmlLoader.getCtArray() },
						{type: 'list',  dataIndex: 'ctBusinessType', options: ctBusinessTypeXmlLoader.getCtArray() }
					]});
    
    function renderSelectItem(value, p, record){
        return String.format('<a href="javascript: selectArmorCallback(\'{0}\', \'{1}\',\'{2}\', \'{3}\',\'{4}\', \'{5}\',\'{6}\', \'{7}\',\'{8}\', \'{9}\',\'{10}\');armorWindow.hide();"><img src="../../images/icons/arrow_rotate_clockwise.png" title="' + message.get('message.list.select') + '" /></a>', 
        record.data.id, record.data.reference, record.data.ctArmorType,record.data.photo1Name,record.data.ctArmor1Type,record.data.armorTypeE,record.data.ctArmorSet);
    }
    
    var armorExpander = new Ext.grid.RowExpander({
         tpl : new Ext.XTemplate(
						'<p><img src="../../_temp/{photo1Name}" width="150px" style="cursor: hand;" onclick="openImageViewer(this.src);"/> &nbsp;&nbsp;&nbsp;&nbsp;<img src="../../_temp/{photo2Name}" width="150px" style="cursor: hand;" onclick="openImageViewer(this.src);"/></p><br/>',
						//'&nbsp;&nbsp;&nbsp;&nbsp;<img src="../../_temp/{photo3Name}" width="150px"/>&nbsp;&nbsp;&nbsp;&nbsp;<img src="../../_temp/{photo4Name}" width="150px"/>',
						'<br/><br/><p><b>' + message.get('message.edit.comment') + ':</b> {comment}</p><br/>'
			        )
    });
    
    // create the grid
    armorGrid = new Ext.grid.GridPanel({
        title: armorMessage.get('ui.armor.listtitle'),
        store: armorStore,
        stripeRows: true,
        plugins: [armorFilters, armorExpander],
        height: 560,
        columns: [armorExpander,{
            header: message.get('message.list.id'),
            width: 20,
            dataIndex: 'index',
            sortable: false
        }, {
            header: message.get('message.edit.reference'),
            width: 80,
            dataIndex: 'reference',
            sortable: true
        }, {
            header: armorMessage.get('ui.armor.type'),
            dataIndex: 'ctArmorType',
            sortable: true
        }, {
            header: '',
            dataIndex: 'ctArmor1Type',
            sortable: true,
            renderer:function(value, p, record){
            		return value + ' ' +record.data.armorTypeE
            }
        }, {
            header: '',
            dataIndex: 'ctArmorSet',
            sortable: true
        }, {
            header: message.get('message.list.select'),
            width: 30,
            dataIndex: 'id',
            sortable: false,
            renderer: renderSelectItem
        }],
        viewConfig: {
            forceFit: true
        },
        loadMask: true,
        renderTo: 'armorDiv',
        bbar: new Ext.PagingToolbar({
            pageSize: 20,
            store: armorStore,
            displayInfo: true
        })
    });
    
    var startPage = getPrarmeterValue(window.location, 'startPage');
    var start;
    if (startPage == null || startPage == '') {
        start = 0;
    }
    else {
        start = (startPage - 1) * 20;
    }
    armorStore.load({
        params: {
            start: start,
            limit: 20
        }
    });
}

